import { createRouter, createWebHistory } from 'vue-router';
 
const routes = [
    {
        path: '/',
        component: () => import('../views/Home.vue'),
        redirect: '/home',
        children: [
            {
                path: '/home',
                name: 'main',
                component: () => import('../views/Main.vue'),
                meta: {
                    title: '首页'
                },
            },
            {
                path: '/resources',
                name: 'resources',
                component: () => import('../views/Resources.vue'),
                meta: {
                    title: '资源'
                }
            },
            {
                path: '/community',
                name: 'community',
                component: () => import('../views/Community.vue'),
                meta: {
                    title: '交流社区'
                }
            },
            {
                path: '/detail',
                name: 'detail',
                component: () => import('../components/Detail.vue')
            }
        ]
    },
    {
        path: '/login',
        component: () => import('../views/Login.vue'),
        meta: {
            title: '登 录'
        }
    },
    // 404路由
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: () => import('../views/NotFound.vue'),
        meta: {
            title: '404—NotFound'
        }
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = to.meta.title
    } else {
        document.title = '码酷——励志成为更好的学习平台'
    }
    next()
});


export default router